// Register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    once:'',
    account:'',
    password: '',
    name: '',
    mobile: '',
    weixin: '',
    department: '',
    grade: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  formSubmit: function (e) {
    var that=this;
    //console.log(e.detail.value);
    if (e.detail.value.account.length == 0 || e.detail.value.account.length >= 8) {
      wx.showToast({
        title: '账号不能为空或过长!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.password.length == 0) {
      wx.showToast({
        title: '密码不能为空!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.name.length == 0) {
      wx.showToast({
        title: '姓名不能为空!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.mobile.length == 0 || e.detail.value.mobile.length>13) {
      wx.showToast({
        title: '请填写正确的电话号码!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.weixin.length == 0) {
      wx.showToast({
        title: '微信不能为空!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.department.length == 0) {
      wx.showToast({
        title: '院系不能为空!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    } else if (e.detail.value.grade.length == 0) {
      wx.showToast({
        title: '班级不能为空!',
        icon: 'loading',
        duration: 1500
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    }else {
      that.setData({
        account: e.detail.value.account,
        password: e.detail.value.password,
        name: e.detail.value.name,
        mobile: e.detail.value.mobile,
        weixin: e.detail.value.weixin,
        department: e.detail.value.department,
        grade: e.detail.value.grade
      })
      this.modalinput()


     
    }
  },
  
  
  modalinput: function () {

    this.setData({

      hiddenmodalput: !this.data.hiddenmodalput

    })

  },

  //取消按钮

  cancel: function () {

    this.setData({

      hiddenmodalput: true

    });

  },

  //确认

  confirm: function () {//确定提交数据

    this.setData({

      hiddenmodalput: true

    })
    if(this.data.once==this.data.password){
      var that=this;
      wx.request({
        url: 'http://47.103.21.65:5645/user/register',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          account: that.data.account,
          password:that.data.password,
          name:that.data.name,
          mobile: that.data.mobile,
          weixin: that.data.weixin,
          department:that.data.department,
          grade: that.data.grade

        },
        success: function (res) {
          console.log(res.data);
          if (res.data.status == 0) {
            console.log(res)
            wx.showToast({
              title: '提交失败！！！',
              icon: 'loading',
              duration: 1500
            })
          } else if (res.data.code == -1) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
          }
          else {
            wx.showToast({
              title: '注册成功！！！',//这里打印出登录成功
              icon: 'success',
              duration: 1000
            })
            wx.request({
              url: 'http://47.103.21.65:5645/user/login',
              method: "POST",

              data: {  //参数传递
                account: that.data.account,
                password: that.data.password,
              },
              header: { //设置请求头
                'content-type': 'application/x-www-form-urlencoded',// 默认值//header:{...}用啥设置啥，

              },

              success: function (res) { //请求成功
                console.log(res.data);

                if (res.data.code == 1) {//登录成功操作
                 
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 2
                    })
                  }, 1000);
                  wx.setStorageSync("token", res.data.data.token)

                  let token = wx.getStorageSync('token')
                  wx.request({

                    url: 'http://47.103.21.65:5645/product/login',
                    method: 'GET',
                    header: {
                      'content-type': 'application/x-www-form-urlencoded',
                      'token': token
                    },
                    success: function (res) {
                      console.log(res)
                    },
                    fail: function (res) {
                      console.log(res)
                    }
                  })


                  console.log(res.data.data.token)
                }
                if (res.data.code == -1) {//登录失败
                  wx.showToast({
                    title: res.data.msg,
                    icon: "none",
                    duration: 2000
                  })
                }
             
              },
              fail: function (error) { //请求失败
                console.log(error);
                console.log(that.data.username);
              }
            })
          
        
          }
        }
      })

    }else{
      wx.showToast({
        title: '两次输入的密码不一致',//这里打印出登录成功
        icon: 'none',
        duration: 1000
    })
    }
  },





  setonce:function(e){
    console.log(e)
      this.setData({
        once:e.detail.value
      })
  }
})
